package cn.wyj.service.impl;

import cn.wyj.dao.ArticleMapper;
import cn.wyj.dao.StatisticMapper;
import cn.wyj.domain.Article;
import cn.wyj.domain.Statistic;
import cn.wyj.service.ArticleService;
import cn.wyj.service.CommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vdurmont.emoji.EmojiParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
//@Transactional
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private StatisticMapper statisticMapper;
    @Autowired
    private CommentService commentService;

    /**
     * 分页查询文章列表
     * @param currentPage
     * @param count
     * @return
     */
    public PageInfo<Article> findArticlesByPage(Integer currentPage,Integer count){
        //1.设置分页查询当前页码和页面数量
        PageHelper.startPage(currentPage,count);
        List<Article> articleList = articleMapper.findByPage();
        //封装statistic统计信息到article对象中
        for (Article article:articleList){
            Statistic statistic = statisticMapper.findStatisticByArticleId(article.getId());
            article.setHits(statistic.getHits());
            article.setCommentsNum(statistic.getCommentsNum());
        }
        PageInfo<Article> pageInfo = new PageInfo<>(articleList);
        return pageInfo;
    }

    /**
     * 得到热度排名前十的文章信息
     * @return
     */
    public List<Article> getHeatArticles(){
        List<Article> articleList = new ArrayList<>();
        //1.得到排序后的统计信息
        List<Statistic> statistics = statisticMapper.getStatistic();
        //2.根据排序后的统计信息，依次封装文章信息
        for(int i = 0;i<statistics.size();i++){
            Statistic statistic = statistics.get(i);
            Article ariticle = articleMapper.findAriticleById(statistic.getArticleId());
            ariticle.setHits(statistic.getHits());
            ariticle.setHits(statistic.getCommentsNum());
            articleList.add(ariticle);
            //2.1 当i=9时，已经得到排名前十的文章信息，跳出循环
            if (i>=9){
                break;
            }
        }
        return articleList;
    }

    /**
     * 查询文章
     * @param id
     * @return
     */
    @Override
    public Article findById(Integer id) {
        return articleMapper.findAriticleById(id);
    }

    /**
     * 发表文章
     * @param article
     */
    @Override
    public void publishArticle(Article article) {
        //去除表情
        article.setContent(EmojiParser.parseToAliases(article.getContent()));
        //补充文章系统属性
        article.setCreated(new Date());
            article.setHits(0);
        article.setCommentsNum(0);
        articleMapper.addArticle(article);
        statisticMapper.addStatistic(article);
    }

    /**
     * 根据主键更新文章
     * @param article
     */
    @Override
    public void updateArticleById(Article article) {
        article.setModified(new Date());
        articleMapper.updateArticle(article);
    }

    /**
     * 根据id删除文章
     * @param id
     */
    @Override
    public void delArticle(Integer id) {
        articleMapper.delArticleById(id);
        //删除文章对应统计数据
        statisticMapper.delStatisticByArticleId(id);
        //删除文章对用的评论数据
        commentService.delComment(id);
    }
}
